package com.yongxing.plugintwo;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class MainActivity extends AppCompatActivity {

    private HookUtil mHookUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.STORAGE)
                .start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHookUtil != null) {
            mHookUtil.onDestroy();
            mHookUtil = null;
        }
    }

    public void startHostActivity(View view) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(getPackageName(),
                "com.yongxing.plugintwo.TestActivity"));
        startActivity(intent);
    }

    public void initHookUtil(View view) {
        if (mHookUtil == null) {
            mHookUtil = new HookUtil();
            mHookUtil.getPackageInfo(this);
            mHookUtil.hookStartActivity(this);
            mHookUtil.hookLaunchActivity();
//            mHookUtil.injectPluginClass(this);
            mHookUtil.putLoadedAPk();
        }
    }

    public void startPluginActivity(View view) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.yongxing.plugin",
                "com.yongxing.plugin.PluginOneActivity"));
        startActivity(intent);

//        try {
//            Object o = getClassLoader().loadClass("com.yongxing.plugin.PluginOneActivity").newInstance();
//        } catch (Exception e) {
//            Log.e("jiang", e.getMessage());
//            e.printStackTrace();
//        }
    }
}
